package com.test;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * type1 的数据源
 *
 * @author 徒有琴
 */
@Configuration
@MapperScan(basePackages = "com.test.dao.d1", sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class DataSource1Config {
    //主数据源配置 ds1数据源
    @Primary
    @Bean(name = "d1-properties")
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSourceProperties dataSourceProperties1() {
        return new DataSourceProperties();
    }

    //主数据源 ds1数据源
    @Primary //这个必须加，
    @Bean(name = "dataSource1")
    public DataSource dataSource1(@Qualifier("d1-properties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    //主数据源 ds1数据源
    @Primary
    @Bean(name = "ds1SqlSessionFactory")
    public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
//        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
//                getResources("classpath*:com/web/ds1/**/*.xml"));
        return sqlSessionFactory.getObject();
    }
}
